package com.person.jc.service.test.certificate;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.common.SQLSpell;
import com.person.jc.service.Service;
import com.person.jc.utils.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by ailk on 2017/10/1.
 */
public class SendService extends Service {
    public final static SendService me = new SendService();

    public List<Record> getList(Map<String, Object> map) {
        String temp = "";

        String tabName = "bs_certificate";
        String whereExt="";

        List<Object> params = new ArrayList<Object>();
        String tempSql = "SELECT\n" +
                "    o.order_id,\n" +
                "    o.order_no,\n" +
                "    o.cert_send_status,\n" +
                "    o.cert_send_date,\n" +
                "    o.cert_send_mode,\n" +
                "    o.consignee_man,\n" +
                "    o.consignee_phone,\n" +
                "    o.address,\n" +
                "    o.cert_ex,\n" +
                "    o.receive_date,\n" +
                "    o.cust_id,\n" +
                "	("+ SQLSpell.customerName("o.cust_id")+") cust_name,\n" +
                "    oe.device_type,\n" +
                "	("+SQLSpell.kindName("device_type", "oe.device_type")+") device_type_name,\n" +
                "    COUNT(oe.device_no) num1,\n" +
                "    COUNT(rl.id) num2\n" +
                "FROM\n" +
                "    bs_order_equipment oe \n" +
                "        inner JOIN\n" +
                "    bs_order o ON oe.order_id = o.order_id\n" +
                "        inner JOIN\n" +
                "    "+tabName+" rl ON oe.equ_id = rl.equ_id\n" + whereExt +
                "WHERE\n" +
                "     o.flow_id = ?\n";

        StringBuffer sql = new StringBuffer(tempSql);
        params.add(map.get("flow"));

        if(Utils.isNotEmpty(temp = String.valueOf(map.get("custId")))){
            sql.append("AND o.cust_id = ?\n");
            params.add(temp);
        }
        if(Utils.isNotEmpty(temp = String.valueOf(map.get("orderId")))){
            sql.append("AND o.order_id like ?\n");
            params.add("%"+temp+"%");
        }
        if(Utils.isNotEmpty(temp = String.valueOf(map.get("deviceType")))){
            sql.append("AND oe.device_type = ?\n");
            params.add(temp);
        }
        sql.append("GROUP BY o.order_id,o.cust_id , oe.device_type \n");
        sql.append("ORDER BY o.order_id,o.cust_id , oe.device_type");
        return Db.find(sql.toString(), Utils.getObjParam(params));
    }

    public int updateSendInfo(Map<String,Object> map){
        String sql = "update bs_order set cert_send_status = 1, cert_send_date=?, cert_send_mode =?, cert_ex=?, cert_send_remark=? where order_id=?";
        return Db.update(sql, map.get("cert_send_date"),map.get("cert_send_mode"),map.get("cert_ex"),map.get("cert_send_remark"),map.get("order_id"));
    }

    public Record findOrderById(String orderId){
        String sql="select link_man, link_mobile, link_phone, address,consignee_man, consignee_phone from bs_order where order_id=?";

        return Db.findFirst(sql,orderId);

    }



}
